package com.example.btmac;

import java.util.ArrayList;
import java.util.List;

import com.example.btmac.R;

import bean.BlueBean;
import utils.BlueUtils;
import utils.BlueUtils2;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SecendActivity extends Activity {

	private TextView mac;
	private TextView name;
	private TextView tv1;
	private TextView tv2;
	private ListView lv;
	private BlueAdapter2 adapter;

	private BlueUtils2 blueUtils;
	private BluHandler handler;

	class BluHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				blueUtils.stopBlue();
				Toast.makeText(getApplication(), "停止扫描", 0).show();
				break;
			}
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_secend);
		handler = new BluHandler();
		blueUtils = new BlueUtils2();
		// 初始化工具类
		blueUtils.getInitialization(this);
		// 判断是否支持蓝牙
		if (!blueUtils.isSupportBlue()) {
			Toast.makeText(this, "设备支持蓝牙4.0", Toast.LENGTH_SHORT).show();
			blueUtils.getmBluetoothAdapter().enable();
		} else {
			Toast.makeText(this, "设备不支持蓝牙4.0", Toast.LENGTH_SHORT).show();
			// 静默开启蓝牙
		}

		init();
		//开始扫描蓝牙
		blueUtils.startBlue();
		// 显示已链接设备MAC
		getMac();
		
		//20秒后关闭扫描
		Message message = new Message();
		message.what = 1;
		handler.sendMessageDelayed(message, 2000*10);
	}

	private void init() {
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		name = (TextView) findViewById(R.id.tv_name);
		mac = (TextView) findViewById(R.id.tv_mac);

		lv = (ListView) findViewById(R.id.lv);

		new ArrayList<BluetoothDevice>();
		
		blueUtils.setCallback2(new BlueUtils2.Callbacks2() {
			@Override
			public void CallbackList(List<BlueBean> mBlueLis) {
				// 我们在此处设置Adapter
				if (adapter == null) {
					adapter = new BlueAdapter2(getApplication(), mBlueLis);
					lv.setAdapter(adapter);
				} else {
					adapter.notifyDataSetChanged();
				}
			}
		});
	}

	private void getMac() {
		BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
		int a2dp = adapter.getProfileConnectionState(BluetoothProfile.A2DP);
		int headset = adapter
				.getProfileConnectionState(BluetoothProfile.HEADSET);
		int health = adapter.getProfileConnectionState(BluetoothProfile.HEALTH);

		int flag = -1;
		if (a2dp == BluetoothProfile.STATE_CONNECTED) {
			flag = a2dp;
		} else if (headset == BluetoothProfile.STATE_CONNECTED) {
			flag = headset;
		} else if (health == BluetoothProfile.STATE_CONNECTED) {
			flag = health;
		}

		if (flag != -1) {
			adapter.getProfileProxy(SecendActivity.this,
					new BluetoothProfile.ServiceListener() {

						@Override
						public void onServiceDisconnected(int profile) {

						}

						@Override
						public void onServiceConnected(int profile,
								BluetoothProfile proxy) {
							// TODO Auto-generated method stub
							List<BluetoothDevice> mDevices = proxy
									.getConnectedDevices();
							if (mDevices != null && mDevices.size() > 0) {
								for (BluetoothDevice device : mDevices) {
									tv1.setVisibility(View.VISIBLE);
									tv2.setVisibility(View.VISIBLE);
									mac.setVisibility(View.VISIBLE);
									name.setText(device.getName());
									mac.setText(device.getAddress());
								}
							} else {
								tv1.setVisibility(View.INVISIBLE);
								tv2.setVisibility(View.INVISIBLE);
								mac.setVisibility(View.INVISIBLE);
								name.setText("没有蓝牙设备");
							}
						}
					}, flag);
		} else {
			tv1.setVisibility(View.INVISIBLE);
			tv2.setVisibility(View.INVISIBLE);
			mac.setVisibility(View.INVISIBLE);
			name.setText("没有蓝牙设备");
		}
	}
}
